package com.eagle.support.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;

import com.kinstalk.voip.sdk.common.Log;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by gc on 2017/1/5.
 */

public class Utility {
    public static final String TAG = "Utility";
    private static final int PHONE_ORIENTATION_UP = 3;
    private static final int PHONE_ORIENTATION_LEFT = 0;
    private static final int PHONE_ORIENTATION_DOWN = 1;
    private static final int PHONE_ORIENTATION_RIGHT = 2;

    public static int getDirection(int ax, int ay, int az) {
        int result = PHONE_ORIENTATION_UP;
        if (ay == 0) {

        }

        if (ax >= 0 && ay >= 0) {
            if (ax > ay) {
                result = PHONE_ORIENTATION_LEFT;
            } else {
                result = PHONE_ORIENTATION_DOWN;
            }
        } else if (ax <= 0 && ay >= 0) {
            if (Math.abs(ax) > ay) {
                result = PHONE_ORIENTATION_RIGHT;
            } else {
                result = PHONE_ORIENTATION_DOWN;
            }
        } else if (ax <= 0 && ay <= 0) {
            if (Math.abs(ax) > Math.abs(ay)) {
                result = PHONE_ORIENTATION_RIGHT;
            } else {
                result = PHONE_ORIENTATION_UP;
            }
        } else if (ax >= 0 && ay <= 0) {
            if (ax > Math.abs(ay)) {
                result = PHONE_ORIENTATION_LEFT;
            } else {
                result = PHONE_ORIENTATION_UP;
            }
        } else {
            Log.e(TAG, "WRONG!");
        }

        return result;
    }

    public static String getFormatSize(long size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }

    public static boolean isAppForeground(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = context.getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = manager.getRunningAppProcesses();

        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    public static String getInstanceId(Context ctx) {
        TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        return "<" + imei + "_" + imei + ">";
    }

    public static String getHttpUserAgent(Context context) {
        String VALUE_SPLIT = "%5";
        String software = "Weaver";
        String version = getAppVersionName(context);
        String oemTag = "";
        String platform = "PHONE";
        String deviceModel = android.os.Build.MODEL;
        StringBuilder ua = new StringBuilder();
        ua.append(software).append(VALUE_SPLIT).append(version == null ? "" : version).append(VALUE_SPLIT).append(oemTag).append(VALUE_SPLIT).append(platform).append(VALUE_SPLIT)
                .append(deviceModel == null ? "" : deviceModel.replace(" ", "_"));
        return ua.toString();
    }

    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Have't version name!");
        } catch (RuntimeException e) {
            Log.e(TAG, "exception when get version name.");
        }
        return versionName;
    }


    /**
     * 打印一些机型信息
     */
    public static void printPhoneInfo() {
        // BOARD 主板
        String phoneInfo = "BOARD: " + android.os.Build.BOARD;
        phoneInfo += ", BOOTLOADER: " + android.os.Build.BOOTLOADER;
        // BRAND 运营商
        phoneInfo += ", BRAND: " + android.os.Build.BRAND;
        phoneInfo += ", CPU_ABI: " + android.os.Build.CPU_ABI;
        phoneInfo += ", CPU_ABI2: " + android.os.Build.CPU_ABI2;
        // DEVICE 驱动
        phoneInfo += ", DEVICE: " + android.os.Build.DEVICE;
        // DISPLAY Rom的名字 例如 Flyme 1.1.2（魅族rom） &nbsp;JWR66V（Android
        // nexus系列原生4.3rom）
        phoneInfo += ", DISPLAY: " + android.os.Build.DISPLAY;
        // 指纹
        phoneInfo += ", FINGERPRINT: " + android.os.Build.FINGERPRINT;
        // HARDWARE 硬件
        phoneInfo += ", HARDWARE: " + android.os.Build.HARDWARE;
        phoneInfo += ", HOST: " + android.os.Build.HOST;
        phoneInfo += ", ID: " + android.os.Build.ID;
        // MANUFACTURER 生产厂家
        phoneInfo += ", MANUFACTURER: " + android.os.Build.MANUFACTURER;
        // MODEL 机型
        phoneInfo += ", MODEL: " + android.os.Build.MODEL;
        phoneInfo += ", PRODUCT: " + android.os.Build.PRODUCT;
        phoneInfo += ", RADIO: " + android.os.Build.getRadioVersion();
        phoneInfo += ", RADITAGSO: " + android.os.Build.TAGS;
        phoneInfo += ", TIME: " + android.os.Build.TIME;
        phoneInfo += ", TYPE: " + android.os.Build.TYPE;
        phoneInfo += ", USER: " + android.os.Build.USER;
        // VERSION.RELEASE 固件版本
        phoneInfo += ", VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
        phoneInfo += ", VERSION.CODENAME: " + android.os.Build.VERSION.CODENAME;
        // VERSION.INCREMENTAL 基带版本
        phoneInfo += ", VERSION.INCREMENTAL: "
                + android.os.Build.VERSION.INCREMENTAL;
        // VERSION.SDK SDK版本
        phoneInfo += ", VERSION.SDK_INT: " + android.os.Build.VERSION.SDK_INT;
        Log.e(TAG, phoneInfo);
    }
}
